package com.example.androidkotlinmvvm.mvvm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.launch

class User2ViewModel: ViewModel() {
    private val userRepository = UserRepository()

    private val _user = MutableStateFlow<Data>(Data(null))
    val user: StateFlow<Data> = _user

    init {
        fetchUsers()
    }

    private fun fetchUsers() {
        viewModelScope.launch {
            userRepository.getUsers().collect {
                _user.value = it
            }
        }
    }
}